define(["require",
	'config/appregister',
	"business/services/constant",
	"jquery",
	'business/postbar/service/postbarhttp',
	"business/home/directives/editorDirective",
	"business/services/fileuploader",
	"business/home/directives/editorDirective",
	"domReady",
	"business/home/services/filterService"], function(require, app,constant,$,httpServices) {
	app.controller('iaskCtrl', function($scope,DetailFilter,$http,$state) {
		//标签
		$scope.questionTitle='';
		$scope.tags2 = [//ngInputTab自己写方法
			
        ];
        $scope.saveTagsname=function(a){
            var l = a.length;
            var blen = 0;
            for(var i=0; i<l; i++) {
                if ((a.charCodeAt(i) & 0xff00) != 0) {
                    blen ++;
                }
                blen ++;
            }
            if(blen >= 50){
                blen = 50;
            }
            if(a == ''){
                return;
            }
        	if($scope.tags2.length == 6){
        		alert('最多只能添加六个关键词哦，亲');
        		return;
        	}
            if($scope.tags2.indexOf(a) != -1){
                alert("关键词不能相同哦");
            }else{
                $scope.tags2.push(a);
                $scope.tagsWork = '';
            }
        }
        $scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
        };

		$scope.checkLength=function(){
			var val = $scope.questionTitle;
			var len = 0;
			for(var i=0; i<val.length; i++) {
				if (val.charCodeAt(i) > 255) {
					len ++;
				}
				len ++;
			}
			if(len >=200){
				$(".iaskTip").show().css("display","block").html("最多输入200字节");
				return false;
			}else{
				$(".iaskTip").hide();
				return true;
			}
		}

		$scope.$parent.isDetailPage = true;
		$scope.$parent.isMainpage = false;
		$scope.getscid = function(scid, $index, tcId) {
			$(".dandan").css("color","#666");
			$(".dandan").eq($index).css("color","#30a3ff");
			
//			if($index == 0) {
//				$(".h21").html("国家：");
//				$(".h22").html("专业：");
//				$(".h23").html("种类：");
//				$(".h24").hide();
//				$(".yiwai").hide();
//			}else if($index == 1){
//				$(".h21").html("专业：");
//				$(".h22").html("内容：");
//				$(".h23").html("项目模式：");	
//				$(".h24").hide();
//				$(".yiwai").hide();
//			}else if($index == 2){
//				$(".h24").show();
//				$(".yiwai").show();
//				$(".h21").html("经济环境：");
//				$(".h22").html("法律法规：");
//				$(".h23").html("合同管理：");	
//				$(".h24").html("宗教文化：");	
//			}		
			$scope.data.scId = scid;
			
//			var thebox = $(".dandan");
//			var max = thebox[$index];
//			var ss = $(max).next();
//			var ele = $(".showbox");
//			ele.hide();
//			ss.show();
//			var zhuanye = [];
//			var leirong = [];
//			var moshi = [];
//			var zongj = [];
			
//			var req = {
//				type:'GET',
//				uri:'getDetailCategory',
//				data:{
//					scId:scid,
//					tcId:tcId
//				}
//			};
//			httpServices.oneclassHttpService(req).success(function(data){
//				if(!data)return;
//				$scope.$apply(function(){
//					var four = data.data;
//						for(i in four) {
//							if(four[i].dcType == 1 || four[i].dcType == 4 || four[i].dcType == 7) {
//								zhuanye.push(four[i]);
//							} else if(four[i].dcType == 2 || four[i].dcType == 5 || four[i].dcType == 8) {				
//								leirong.push(four[i]);
//							} else if(four[i].dcType == 3 || four[i].dcType == 6 || four[i].dcType == 9) {
//								moshi.push(four[i]);
//							} else if(four[i].dcType == 10) {
//								zongj.push(four[i]);
//							}
//						}
//						$scope.zy = zhuanye;
//						$scope.lr = leirong;
//						$scope.ms = moshi;
//						$scope.duo = zongj;
//				});
//			}).error(function(){
//				alert("请求失败");
//			});
		};
		//
		$scope.closex = function(){
			$("#oked").hide();
		};

		//
		$scope.getthis = function($index, dcid) {
			var qunsed = $(".iask-three ul li");
			for(var i = 0; i < qunsed.length; i++) {
				qunsed[i].style.color = "#666";
			}
			qunsed[$index].style.color = "#30a3ff";
			$scope.data.dcId = dcid;
		};
		$scope.upload = {
			url:constant.config.BASEURL_UPLOADER,
			api:constant.config.CONSTANT_UPLOADER,
			path:"",
			file:"",
			onchange:function(){
				//$scope.$apply(function(){
				//    $scope.showTemplate.isSelectFile = true;
				//    $scope.showTemplate.isUploadFile = false;
				//});http://192.168.1.14:8083/elink_uploader/fileUpload"
			}
		};
		$scope.$on("uploadSuccess",function(a,b){
			var html = '<img src="them/face/01.gif">';
			var id ="editorbox1";
			$("#"+id).focus();
			var c = b;
			constant.insertImg(html,id)
		});
		
		//
		$scope.getthree = function(scid, $index,tcId) {
			var quns = $(".iask-pleaseed ul li");
			for(var i = 0; i < quns.length; i++) {
				quns[i].style.color = "#666";
			}
			quns[$index].style.color = "#30a3ff";
//			var req = {
//				type:'GET',
//				uri:'getDetailCategory',
//				data:{
//					scId:scid,
//					tcId:tcId
//				}
//			};
//			httpServices.oneclassHttpService(req).success(function(data){
//				if(!data)return;
//				$scope.$apply(function(){
//					if(data.code==0&&data.data!==null) {
//						if(data.data.length>0) {
//							$(".iask-three").show();
//							$scope.three = data.data;						
//						}else {
//							$(".iask-three").hide();
//							console.log(data.data);
//						}
//					}else {
//						console.log(data.msg);
//					}
//					
//				});
//			}).error(function(){
//				alert("渲染三级分类失败");
//			});
			$scope.data.scId = scid;
		};
		
		//
		$scope.getsecond = function(tcid, $index) {
			var qun = $(".iask-please ul li");
			for(var i = 0; i < qun.length; i++) {
				qun[i].style.color = "#666";
			}
			qun[$index].style.color = "#30a3ff";
			if(tcid == 2) {
				$(".iask-pleaseed").hide();
				$(".iask-three").hide();
				$(".iask-them").show();
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:tcid
					}
				};
				httpServices.oneclassHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0&&data.data.length>0) {
							$scope.them = data.data;					
						}else {
							console.log(data.msg);
						}					
					});
				}).error(function(){
					alert("渲染国际工程里的list失败");
				});				
			} else {
				$(".iask-them").hide();
				$(".iask-three").hide();
				$(".iask-pleaseed").show();
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:tcid
					}
				};
				httpServices.oneclassHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0&&data.data.length>0) {
							$scope.twolist = data.data;					
						}else {
							console.log(data.msg);
						}					
					});
				}).error(function(){
					alert("渲染二级分类失败");
				});						
			}
				$scope.data.tcId = tcid;
		};

		//第一次渲染list
		getonelist();
		function getonelist(){
			var req = {
				type:'GET',
				uri:'getTopCategory'
			};
			httpServices.oneclassHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0&&data.data.length>0) {
						$scope.onelist = data.data;				
					}else {
						console.log(data.msg);
					}					
				});
			}).error(function(){
				alert("第一次渲染list失败");
			});				
		};	
		
		//获取用户信息
		getUserInfo();
		function getUserInfo() {
			$.ajax({
				type:"get",
				url:"http://"+allUrl+":8080/elink_user/api/user/getBaseUeserInfo?seed="+localStorage.seed+"&authToken="+localStorage.authToken,
				success:function(data) {
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							$scope.userCionValue = data.data.coinValue;
						}else {
							console.log(data.msg);
						}
					});
				},
				error: function(){
					console.log("请求用户信息失败")
				}
			});
		};
		
		//用户余额相关提示
		$scope.onCion = function(){
			$("#cion").hide();
		}
		
		//初始化公共参数
		$scope.data = {
			"title": "",
			"content": "",
			"labelIds": "",
			"tcId": "",
			"scId": "",
			"dcId": "",
			"price": "",
			"remindType": ""
		};
		
		$scope.subPost = function() {
			if($scope.tags2.join(',')==="")return alert("请添加标签"); 
			var rad1 = $('.showbox input[name="radio1"]:checked').val();
			var rad2 = $('.showbox input[name="radio2"]:checked').val();
			var rad3 = $('.showbox input[name="radio3"]:checked').val();
			var title = $scope.questionTitle;
			$scope.content = $("#editorbox1").html();
			var price = $('.price').val();
			
			if(!!!price || price == null || price == undefined || price == ""){
				price = 0;
			}
			var tab = $('#radio-one input[name="radio"]:checked').val();
			//提交表单前的标签转换请求
			/*var num = 0;
			for(var i = 0;i<$scope.tags2.join(',').length;i++){
				if($scope.tags2.join(',')[i]==","){
					num++;
				}
			}*/
			if($scope.userCionValue < price){
				$("#cion").show();
				return;
			}			
			if($scope.tags2.length>6) {
				return alert("最多添加6个标签！");
			}
			if(title==''){
				alert("问题标题不能为空");
				return;
			}else{
				if(!$scope.checkLength()){
					alert('问题标题最多可输入200字节');
					return;
				}
			}
			if(!!$scope.content && $scope.content.length > 4000) {
				alert("最多输入2000个字！");
				return;
			}			
			if($scope.data.tcId == "" || $scope.data.scId == "") {
				alert("请选择准确的分类")
				return false;
			}				
			var req = {
				type:'POST',
				uri:'add',
				data:{
					labels:$scope.tags2.join(',')
				}
			};
			httpServices.gettagHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						$scope.data.labelIds = data.data;
						//下面是提交后第2个请求
						if($scope.data.dcId) {
							if(!$scope.data.dcId){
								$scope.data.dcId = 0;
							}
							var req = {
								type:'POST',
								uri:'add',
								data:{
									"solve":1,
									"title":title,
									"content": $scope.content,
									"labelIds": $scope.data.labelIds,
									"tcId": $scope.data.tcId,
									"scId": $scope.data.scId,
									"dcId": '',
									"price": price,
									"remindType": tab,
									"seed":localStorage.seed,
									"authToken":localStorage.authToken
								}
							};
							console.log(req.data);
							$.ajax({
								type:"post",
								dataType:'json',
								url:"http://"+allUrl+":8086/elink_postbar/api/bostbar/add",
								data:req.data,
								success:function(data){
								if(!data)return;
									if(data.code==0) {
										$("#oked").show();
										$(".iask-sub-btn").unbind("click");
										 setTimeout(function(){
										 	$state.go("eng.wikilist");
										 },1600)							
									}else {
										console.log(data.msg);
									}									
								},
								error:function(){
									console.log("提交问题请求失败");
								}
							});
						} else if($scope.data.labelIds !== "") {
							if(!rad1)rad1 = 0;
							if(!rad2)rad2 = 0;
							if(!rad3)rad3 = 0;
							var req = {
								type:'POST',
								uri:'add',
								data:{
									"solve":1,
									"title": title,
									"content": $scope.content,
									"labelIds": $scope.data.labelIds,
									"tcId": $scope.data.tcId,
									"scId": $scope.data.scId,
									"dcId": '',
									"price": price,
									"remindType": tab,
									"seed":localStorage.seed,
									"authToken":localStorage.authToken									
								}
							};
							$.ajax({
								type:"post",
								dataType:'json',
								url:"http://"+allUrl+":8086/elink_postbar/api/bostbar/add",
								data:req.data,
								success:function(data){
								if(!data)return;
									if(data.code==0) {
										$("#oked").show();
										$(".iask-sub-btn").unbind("click");
										 setTimeout(function(){
										 	$state.go("eng.wikilist");
										 },1600)							
									}else {
										console.log(data.msg);
									}									
								},
								error:function(){
									console.log("提交问题请求失败");
								}
							});							
						} else {
							console.log("标签不能为空");
						}							
					}else {
						console.log(data.msg);
					}
				});
			}).error(function(){
				alert("标签请求失败");
			});
		};
	});
})